package com.lagou.homework.myZk.utils;

import org.I0Itec.zkclient.ZkClient;

public class ZkUtils {

    public static ZkClient getCon(){
        ZkClient zkClient = new ZkClient("centos7-1:2181");
        return zkClient;
    }

    public static void createNode(ZkClient zkClient,String path){
        // 判断节点是否存在
        boolean exists = zkClient.exists(path);
        String dbConfig = MySqlConfig.JDBCURL + "###" + MySqlConfig.USERNAME + "###" + MySqlConfig.PASSWORD;
        System.out.println("当前数据库配置为：" + dbConfig);
        // 不存在则创建
        if (!exists){
            // 创建节点，把当前连接信息写入zk
            zkClient.createPersistent(path,true);
            zkClient.writeData(path,dbConfig);
            System.out.println("创建了新的节点");
        }else{
            zkClient.writeData(path,dbConfig);
            System.out.println("重写了旧的节点");
        }
        System.out.println(path + " 被创建");
    }
}
